import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Zhang Guangyong
 * @desacription 主程序
 * @date 2019年12月10日 16:15
 */
public class Main {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {


        File path = new File("");
        try {
            String filePath = path.getCanonicalPath();
            File[] files = new File(filePath + "\\").listFiles();
            int i = 0;
            if (files != null) {
                for (File file : files) {
                    String name = file.getName();
                    String suffix = name.substring(name.lastIndexOf(".") + 1);
                    if ("json".equals(suffix)) {
                        String newFile = filePath + "\\" + name.replace(".json", ".xlsx");
                        createExcel(file, newFile);
                        System.out.println("生成文件:" + newFile);
                        i++;
                    }
                }
            }
            System.out.println("处理完成, 生成文件" + i + "个");
        } catch (IOException e) {
            System.out.println("路径错误");
            e.printStackTrace();
        }

//        File file = new File("D://test//arr.json");
//
//        String jsonStr = "";
//
//        String outPath = "D://test//toExcel.xlsx";

//        createExcel(file, outPath);
    }

    @SuppressWarnings("unchecked")
    private static void createExcel(File file, String outPath) {
        String jsonStr;
        try {

            FileReader fileReader = new FileReader(file);
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
            int ch;
            StringBuilder sb = new StringBuilder();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            // 关闭流
            fileReader.close();
            reader.close();

            // 转换为对象
            jsonStr = sb.toString();
            JSONArray array = (JSONArray) JSON.parse(jsonStr);
            //
            Map<String, String> item = (Map<String, String>) array.get(0);
            Set<String> strings = item.keySet();

            XSSFWorkbook workbook = new XSSFWorkbook();

            XSSFSheet sheet = workbook.createSheet("转json");

            XSSFRow row = sheet.createRow(0);

            int i = 0;
            List<String> titles = new ArrayList<>();
            for (String title : strings) {
                XSSFCell cell = row.createCell(i);
                cell.setCellValue(title);
                titles.add(title);
                i++;
            }

            int j = 1;
            for (Object o : array) {
                Map<String, String> map = (Map<String, String>) o;
                row = sheet.createRow(j);
                int k = 0;
                for (String title : titles) {
                    XSSFCell cell = row.createCell(k);
                    cell.setCellValue(map.get(title));
                    k++;
                }
                j++;
            }

            File file1 = new File(outPath);

            FileOutputStream outputStream = new FileOutputStream(file1);

            workbook.write(outputStream);

        } catch (IOException e) {
            System.out.println("读取出错");
        }
    }

}
